﻿/*
 *
 * View.cs
 * For view_name.php in CI
 * 
 * Author @semarketir
 * 
 * CICI is simple tool for generate Controller, View, and View in CodeIgniter ( or CI ) Framework
 * CICI can used to setup configurations in CI ( Database, Autoload, Config ) <- Next Version
 * CICI just can to used for default directory of CodeIgniter ( or CI ) Framework
 * 
 * CICI v.0.0.4 Beta
 * 
 * Support with CI 2.X ( or heigh )
 * 
 * License Under GPL
 * 
 * 
 * Salam, 
 * semar ketir
 * 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace cici
{
    class View
    {
        private string path_application;
        private StreamWriter streamWritter;

        public View()
        {
            this.path_application = Environment.CurrentDirectory + @"\application\views";
        }

        private void createFileView(string file_location, string tempViewName)
        {
            this.streamWritter = new StreamWriter(file_location);
            this.streamWritter.Write(this.templateView(file_location, tempViewName));
            this.streamWritter.Flush();
            this.streamWritter.Close();
        }

        private string[] createFolderView(string tempViewName)
        {

            string[] temp_folder_view_split = tempViewName.Split('.');

            int length_array = temp_folder_view_split.Length - 1;

            string view_name_of_view = "Noview";

            int viewName_of_index_or_array = 0;

            for (int temp_loop = length_array; temp_loop > 0; temp_loop--)
            {
                if (temp_folder_view_split[temp_loop].Length > 0)
                {
                    view_name_of_view = temp_folder_view_split[temp_loop];
                    viewName_of_index_or_array = temp_loop;
                    break;
                }
            }

            string requr_folder_view = "";

            for (int temp_loop2 = 0; temp_loop2 < viewName_of_index_or_array; temp_loop2++)
            {
                if (temp_folder_view_split[temp_loop2].Length > 0)
                {
                    requr_folder_view += @"\" + temp_folder_view_split[temp_loop2];
                }
            }
            requr_folder_view = this.path_application + requr_folder_view;

            Directory.CreateDirectory(requr_folder_view);

            return new string[] { requr_folder_view, view_name_of_view };
        }

        public Boolean createView(string viewName)
        {
            Boolean status = false;

            string[] temps = viewName.Split(';');

            if (Directory.Exists(this.path_application))
            {
                foreach (string tempViewName in temps)
                {
                    if (tempViewName.Length > 0)
                    {
                        Match m = Regex.Match(tempViewName, @"[.]");
                        if (m.Success)
                        {
                            string[] temp_folder_and_view =
                                this.createFolderView(tempViewName);
                            this.createFileView(temp_folder_and_view[0] + @"\" + temp_folder_and_view[1] + ".php",
                                temp_folder_and_view[1]);

                        }
                        else
                        {
                            this.createFileView(this.path_application + @"\" + tempViewName + ".php",
                                tempViewName);
                        }
                    }
                }

                status = true;
            }

            return status;
        }

        protected virtual string templateView(string file_location, string viewName)
        {
            string template =
                "<br /><br />\n"+
                "<div id=\"view-content\">\n" +
                "\t<p>View script <b>" + viewName + "</b> and location : " + file_location + "</p>\n" +
                "</div>\n"+
                "<!-- Generated by CICI -->";

            return template;
        }
    }
}
